package com.st.warehouse.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.st.warehouse.R;
import com.st.warehouse.utils.SystemUtils;

import java.util.ArrayList;

public class GridViewAdapter extends BaseAdapter {
    private ArrayList<String> listUrls;
    private LayoutInflater inflater;
    private Context mContext;

    public GridViewAdapter(ArrayList<String> listUrls) {
        this.listUrls = listUrls;
        if (listUrls.size() == 5) {
            listUrls.remove(listUrls.size() - 1);
        }
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return listUrls.size();
    }

    @Override
    public Object getItem(int position) {
        return listUrls.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        mContext = parent.getContext();
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_pic_item, parent, false);
            holder.image = convertView.findViewById(R.id.imageView);
            holder.myImage_close = convertView.findViewById(R.id.myImage_close);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        //获取图片路径
        final String path = listUrls.get(position);
        //显示图片
        if (path.equals("paizhao")) {
            holder.myImage_close.setVisibility(View.GONE);
        } else {
            holder.myImage_close.setVisibility(View.VISIBLE);
            SystemUtils.glideLoad((Activity) mContext, path, holder.image, 0);
        }
        //图片删除
        holder.myImage_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == 5) {
                    listUrls.remove(listUrls.get(position));
                    listUrls.add("paizhao");
                } else {
                    listUrls.remove(listUrls.get(position));
                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
    public final class ViewHolder {
        ImageView image;//图片
        ImageView myImage_close;//删除
    }
}
